<template>
	<div class="lunbo">
		<div class="l_item">
			<div class="name">
				转发量
			</div>
			<div v-text="zhuanfa" class="num">
			</div>
			<!--<NumberAnimate></NumberAnimate>-->
		</div>
		<div class="l_item">
			<div class="name">
				报备量
			</div>
			<div v-text="baobei" class="num">
			</div>
		</div>
		<div class="l_item big_item">
			<div class="name">
				浏览量
			</div>
			<div class="chartNum">
				<div class="box-item">
					<li :class="{'number-item': !isNaN(item), 'mark-item': isNaN(item) }" v-for="(item,index) in orderNum" :key="index">
						<span v-if="!isNaN(item)">
                        <i ref="numberItem">0123456789</i>
                    </span>
						<span class="comma" v-else>{{item}}</span>
					</li>
				</div>
			</div>
		</div>
		<div class="l_item">
			<div class="name">
				收藏量
			</div>
			<div v-text="shoucang" class="num"></div>
		</div>
		<div class="l_item">
			<div class="name">
				独立访问
			</div>
			<div v-text="fangwen" class="num"></div>
		</div>
	</div>
</template>

<script>
	//	import NumberAnimate from './NumberAnimate'
	import { topBrowseNum, topCollectNum, topForwardNum, topReportNum, topIndepAccessNum } from '@/request/api'; // 导入我们的api接口
	export default {
		name: 'Header',
		components: {
			//			NumberAnimate
		},
		data() {
			return {
				zhuanfa: 0,
				baobei: 0,
				shoucang: 0,
				fangwen: 0,

				orderNum: ['0', '0', '0', '0', '0', '0', '0', '0'], // 默认订单总数
			}
		},
		created() {
			let that = this;
			
			setInterval(function() {
				that.BrowseNum();
			}, 1000 * 30);
			setInterval(function() {
				that.CollectNum();
				that.ForwardNum();
				that.ReportNum();
				that.IndepAccessNum();
			}, 1000 * 40);
			that.BrowseNum();
			that.CollectNum();
			that.ForwardNum();
			that.ReportNum();
			that.IndepAccessNum();
		},
		mounted() {

		},
		methods: {
			BrowseNum() {
				// 调用api接口，并且提供了两个参数      
				// searchType：搜索类型 --- 0今日（按24小时）、1本周（按日）、2本月（按日）、3季度（按日）、4半年（按日）、5今年（按月）
				//searchCity：搜索城市公司（如果该字段为空则查询全国）
				let that = this;
				topBrowseNum({
					searchType: '',
					searchCity: ''
				}).then(res => {
					// 获取数据成功后的其他操作
					var numStr = res.data;
					that.toOrderNum(parseInt(numStr));
				})
			},
			CollectNum() {
				let that = this;
				topCollectNum({
					searchType: '',
					searchCity: ''
				}).then(res => {
					var numStr = res.data;
					that.shoucang = parseInt(numStr)
				})
			},
			ForwardNum() {
				let that = this;
				topForwardNum({
					searchType: '',
					searchCity: ''
				}).then(res => {
					var numStr = res.data;
					that.zhuanfa = parseInt(numStr)
				})
			},
			ReportNum() {
				let that = this;
				topReportNum({
					searchType: '',
					searchCity: ''
				}).then(res => {
					var numStr = res.data;
					that.baobei = parseInt(numStr)
				})
			},
			IndepAccessNum() {
				let that = this;
				topIndepAccessNum({
					searchType: '',
					searchCity: ''
				}).then(res => {
					var numStr = res.data;
					that.fangwen = parseInt(numStr)
				})
			},
			// 设置文字滚动
			setNumberTransform() {
				const numberItems = this.$refs.numberItem // 拿到数字的ref，计算元素数量
				const numberArr = this.orderNum.filter(item => !isNaN(item))
				// 结合CSS 对数字字符进行滚动,显示订单数量
				for(let index = 0; index < numberItems.length; index++) {
					const elem = numberItems[index]
					elem.style.transform = `translate(-50%, -${numberArr[index] * 10}%)`
				}
			},
			// 处理总订单数字
			toOrderNum(num) {
				num = num.toString()
				// 把订单数变成字符串
				if(num.length < 8) {
					num = '0' + num // 如未满八位数，添加"0"补位
					this.toOrderNum(num) // 递归添加"0"补位
				} else if(num.length === 8) {
					// 订单数中加入逗号
					// num = num.slice(0, 2) + ',' + num.slice(2, 5) + ',' + num.slice(5, 8)
					this.orderNum = num.split('') // 将其便变成数据，渲染至滚动数组
				} else {
					// 订单总量数字超过八位显示异常
					this.$message.warning('总量数字过大')
				}
				this.setNumberTransform()
			},

		}
	}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
	.lunbo {
		padding-top: 20px;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	
	.l_item {
		width: 300px;
		height: 100px;
		background: url("../assets/img/lunbo_bg.png") no-repeat center center;
		background-size: 100% 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	
	.l_item.big_item {
		width: 380px;
		height: 130px;
	}
	
	.name {
		font-size: 20px;
		color: #FFFFFF;
		padding: 10px;
	}
	
	.num {
		font-size: 36px;
		color: #2BFAFF;
	}
	
	.big_item .name {
		font-size: 26px;
	}
	
	.big_item .num {
		font-size: 48px;
	}
	/*订单总量滚动数字设置*/
	
	.chartNum {
		display: flex;
		justify-content: center;
	}
	
	.box-item {
		position: relative;
		height: 80px;
		font-size: 54px;
		line-height: 41px;
		text-align: center;
		list-style: none;
		color: #2D7CFF;
		color: #2BFAFF;
		writing-mode: vertical-lr;
		text-orientation: upright;
		-moz-user-select: none;
		-webkit-user-select: none;
		-ms-user-select: none;
		-khtml-user-select: none;
		user-select: none;
	}
	/* 默认逗号设置 */
	
	.mark-item {
		width: 10px;
		height: 100px;
		margin-right: 5px;
		line-height: 10px;
		font-size: 48px;
		position: relative;
	}
	
	.mark-item>span {
		position: absolute;
		width: 100%;
		bottom: 0;
		writing-mode: vertical-rl;
		text-orientation: upright;
	}
	/*滚动数字设置*/
	
	.number-item {
		width: 33px;
		height: 60px;
		/* 背景图片 */
		background: url("../assets/img/text-bg-blue.png") no-repeat center center;
		background-size: 100% 100%;
		list-style: none;
		margin-right: 5px;
		border-radius: 4px;
		/*border: 1px solid rgba(221, 221, 221, 1);*/
	}
	
	.number-item>span {
		position: relative;
		display: inline-block;
		margin-right: 10px;
		font-size: 40px;
		width: 100%;
		height: 100%;
		writing-mode: vertical-rl;
		text-orientation: upright;
		overflow: hidden;
	}
	
	.number-item>span>i {
		font-style: normal;
		position: absolute;
		top: 11px;
		left: 50%;
		transform: translate(-50%, 0);
		transition: transform 1s ease-in-out;
		letter-spacing: 10px;
	}
	
	.number-item:last-child {
		margin-right: 0;
	}
</style>